---
title: "Analysis: Sharing Asymmetry"
output: html_document
---

```{r}

# set r markdown chunk defaults
knitr::opts_chunk$set(warning = F, message = F)

```

# Description

This file imports the cleaned YouGov data (created in 'cleaning.Rmd') and runs the analysis used to create Figures 1, 3, and 7 in the manuscript. 

# Setup

```{r}

# clearing memory ------------------------------------------------------------
rm(list = ls())

# load packages ----------------------------------------------------------------
library(here)
library(tidyverse)
library(ggplot2)
library(survey)
library(srvyr)
library(ggeffects)
library(stargazer)
library(broom)
library(ggstance)
library(ggpubr)
library(arm)
library(gridExtra)
library(doParallel)
library(foreach)
library(broom)
library(margins)
library(patchwork)
library(relimp, pos = 4)
library(paran)
library(clarify)
library(viridis)
library(stringr)


# useful functions -------------------------------------------------------------

# function to calculate standard error of mean
std_error <- function(var){
  sd(var, na.rm = T) / sqrt(length(var[!is.na(var)]))
}

# function to manually bin continous variables
bin_fun <- function(x, bin_size){
  bin_size * ceiling(x / bin_size) - bin_size / 2
}

# function to put variables on a 0-1 scale
rescale_01 <- function(x, max){
  (x-1)/(max-1)
}

# function to compute two-way clustered standard errors with survey weights
source(here("code", "helper functions", "glmweave.R"))

# set n_sim
n_sim <- 1000

# set seed
set.seed(4787)

# plotting colors
cb_black <- "#000000" # Discernment / all headlines
cb_blue <- "#0072B2"  # Democratic headlines
cb_red<- "#D55E00"    # Republican headlines
cb_green = "#009E73"  # True headlines
cb_orange = "#E69F00" # False headlines

```

# Load Data

```{r}

# load cleaned data
long <- read.csv(here("data", "cleaned", "long.csv"))
yougov <- read.csv(here("data", "cleaned", "yougov.csv"))

```


# Main Analysis Without Controls 

## Agreeable Headlines (m1nc)

```{r}

# create dataset
m1nc_data <- 
  long %>% 
  filter(hdl_agreeable == 1, cond_int == "control") %>% 
  dplyr::select(share_01, hdl_true, party_dem, weight, caseid, hdl_id) %>%
  na.omit()

# run model on all headlines
m1nc_all <-  
  glm(share_01 ~ hdl_true*party_dem,
      family = "gaussian", weights = weight, data = m1nc_data)

# run model on true headlines
m1nc_true <-  
  glm(share_01 ~ party_dem,
      family = "gaussian", weights = weight, data = m1nc_data %>% filter(hdl_true == 1))

# run model on false headlines
m1nc_false <-  
  glm(share_01 ~ party_dem,
      family = "gaussian", weights = weight, data = m1nc_data %>% filter(hdl_true == 0))

# stimulation-based inference
m1nc_sim <- 
  clarify::sim(m1nc_all, n = n_sim, vcov = xeffect.glm(glm.obj = m1nc_all, g1 = m1nc_all$data$caseid, g2 = m1nc_all$data$hdl_id))

m1nc_sim_apply <- 
  sim_apply(m1nc_sim, cl = 6,
            function(fit){
              true_right <- predict(fit, newdata = m1nc_data %>% mutate(hdl_true = 1, party_dem = 0), type = "response")
              true_left <- predict(fit, newdata = m1nc_data %>% mutate(hdl_true = 1, party_dem = 1), type = "response")
              false_right <- predict(fit, newdata = m1nc_data %>% mutate(hdl_true = 0, party_dem = 0), type = "response")
              false_left <- predict(fit, newdata = m1nc_data %>% mutate(hdl_true = 0, party_dem = 1), type = "response")
              
              c(true_right = mean(true_right), true_left = mean(true_left), 
                false_right = mean(false_right), false_left = mean(false_left))})

# calculate additive ('diff') and multiplicative ('ratio') discernment quantities
m1nc_transformed <-
  transform(m1nc_sim_apply, 
            `true_right - false_right` = true_right - false_right,
            `true_left - false_left` = true_left - false_left,
            `true_right / false_right` = true_right / false_right,
            `true_left / false_left` = true_left / false_left) %>%
  transform(`diff` = `true_left - false_left` - `true_right - false_right` , 
            `ratio` = `true_left / false_left` / `true_right / false_right`)

```


## Disagreeable Headlines (m2nc)

```{r}

# create dataset
m2nc_data <- 
  long %>% 
  filter(hdl_agreeable == 0, cond_int == "control") %>% 
  dplyr::select(share_01, hdl_true, party_dem, weight, caseid, hdl_id) %>%
  na.omit()

# run model on all headlines
m2nc_all <-  
  glm(share_01 ~ hdl_true*party_dem,
      family = "gaussian", weights = weight, data = m2nc_data)

# run model on true headlines
m2nc_true <-  
  glm(share_01 ~ party_dem,
      family = "gaussian", weights = weight, data = m2nc_data %>% filter(hdl_true == 1))

# run model on false headlines
m2nc_false <-  
  glm(share_01 ~ party_dem,
      family = "gaussian", weights = weight, data = m2nc_data %>% filter(hdl_true == 0))

# simulation-based inference
m2nc_sim <- 
  clarify::sim(m2nc_all, n = n_sim, vcov = xeffect.glm(glm.obj = m2nc_all, g1 = m2nc_all$data$caseid, g2 = m2nc_all$data$hdl_id))

m2nc_sim_apply <- 
  sim_apply(m2nc_sim, cl = 6,
            function(fit){
              true_right <- predict(fit, newdata = m2nc_data %>% mutate(hdl_true = 1, party_dem = 0), type = "response")
              true_left <- predict(fit, newdata = m2nc_data %>% mutate(hdl_true = 1, party_dem = 1), type = "response")
              false_right <- predict(fit, newdata = m2nc_data %>% mutate(hdl_true = 0, party_dem = 0), type = "response")
              false_left <- predict(fit, newdata = m2nc_data %>% mutate(hdl_true = 0, party_dem = 1), type = "response")
              
              c(true_right = mean(true_right), true_left = mean(true_left), 
                false_right = mean(false_right), false_left = mean(false_left))})

# calculate additive ('diff') and multiplicative ('ratio') discernment quantities
m2nc_transformed <-
  transform(m2nc_sim_apply, 
            `true_right - false_right` = true_right - false_right,
            `true_left - false_left` = true_left - false_left,
            `true_right / false_right` = true_right / false_right,
            `true_left / false_left` = true_left / false_left) %>%
  transform(`diff` = `true_left - false_left` - `true_right - false_right` , 
            `ratio` = `true_left / false_left` / `true_right / false_right`)

```

# Main Analysis With Controls 

## Agreeable Headlines (m1wc)

```{r}

# create dataset
m1wc_data <- 
  long %>% 
  filter(hdl_agreeable == 1, cond_int == "control") %>% 
  dplyr::select(share_01, hdl_true, party_dem, weight, caseid, hdl_id,
                age_sd, female, education_sd) %>%
  na.omit()

# run model on all headlines
m1wc_all <-  
  glm(share_01 ~ hdl_true*(party_dem + age_sd + female + education_sd),
      family = "gaussian", weights = weight, data = m1wc_data)

# run model on true headlines
m1wc_true <-  
  glm(share_01 ~ party_dem + age_sd + female + education_sd,
      family = "gaussian", weights = weight, data = m1wc_data %>% filter(hdl_true == 1))

# run model on false headlines
m1wc_false <-  
  glm(share_01 ~ party_dem + age_sd + female + education_sd,
      family = "gaussian", weights = weight, data = m1wc_data %>% filter(hdl_true == 0))

# simulation-based inference
m1wc_sim <- 
  clarify::sim(m1wc_all, n = n_sim, vcov = xeffect.glm(glm.obj = m1wc_all, g1 = m1wc_all$data$caseid, g2 = m1wc_all$data$hdl_id))

m1wc_sim_apply <- 
  sim_apply(m1wc_sim, cl = 6,
            function(fit){
              true_right <- predict(fit, newdata = m1wc_data %>% mutate(hdl_true = 1, party_dem = 0), type = "response")
              true_left <- predict(fit, newdata = m1wc_data %>% mutate(hdl_true = 1, party_dem = 1), type = "response")
              false_right <- predict(fit, newdata = m1wc_data %>% mutate(hdl_true = 0, party_dem = 0), type = "response")
              false_left <- predict(fit, newdata = m1wc_data %>% mutate(hdl_true = 0, party_dem = 1), type = "response")
              
              c(true_right = mean(true_right), true_left = mean(true_left), 
                false_right = mean(false_right), false_left = mean(false_left))})

# calculate additive ('diff') and multiplicative ('ratio') discernment quantities
m1wc_transformed <-
  transform(m1wc_sim_apply, 
            `true_right - false_right` = true_right - false_right,
            `true_left - false_left` = true_left - false_left,
            `true_right / false_right` = true_right / false_right,
            `true_left / false_left` = true_left / false_left) %>%
  transform(`diff` = `true_left - false_left` - `true_right - false_right` , 
            `ratio` = `true_left / false_left` / `true_right / false_right`)

```


## Disagreeable Headlines (m2wc)

```{r}

# create dataset
m2wc_data <- 
  long %>% 
  filter(hdl_agreeable == 0, cond_int == "control") %>% 
  dplyr::select(share_01, hdl_true, party_dem, weight, caseid, hdl_id, 
                age_sd, female, education_sd) %>%
  na.omit()

# run model on all headlines
m2wc_all <-  
  glm(share_01 ~ hdl_true*(party_dem + age_sd + female + education_sd),
      family = "gaussian", weights = weight, data = m2wc_data)

# run model on true headlines
m2wc_true <-  
  glm(share_01 ~ party_dem + age_sd + female + education_sd,
      family = "gaussian", weights = weight, data = m2wc_data %>% filter(hdl_true == 1))

# run model on false headlines
m2wc_false <-  
  glm(share_01 ~ party_dem + age_sd + female + education_sd,
      family = "gaussian", weights = weight, data = m2wc_data %>% filter(hdl_true == 0))

# simulation-based inference
m2wc_sim <- 
  clarify::sim(m2wc_all, n = n_sim, vcov = xeffect.glm(glm.obj = m2wc_all, g1 = m2wc_all$data$caseid, g2 = m2wc_all$data$hdl_id))

m2wc_sim_apply <- 
  sim_apply(m2wc_sim, cl = 6,
            function(fit){
              true_right <- predict(fit, newdata = m2wc_data %>% mutate(hdl_true = 1, party_dem = 0), type = "response")
              true_left <- predict(fit, newdata = m2wc_data %>% mutate(hdl_true = 1, party_dem = 1), type = "response")
              false_right <- predict(fit, newdata = m2wc_data %>% mutate(hdl_true = 0, party_dem = 0), type = "response")
              false_left <- predict(fit, newdata = m2wc_data %>% mutate(hdl_true = 0, party_dem = 1), type = "response")
              
              c(true_right = mean(true_right), true_left = mean(true_left), 
                false_right = mean(false_right), false_left = mean(false_left))})

# calculate additive ('diff') and multiplicative ('ratio') discernment quantities
m2wc_transformed <-
  transform(m2wc_sim_apply, 
            `true_right - false_right` = true_right - false_right,
            `true_left - false_left` = true_left - false_left,
            `true_right / false_right` = true_right / false_right,
            `true_left / false_left` = true_left / false_left) %>%
  transform(`diff` = `true_left - false_left` - `true_right - false_right` , 
            `ratio` = `true_left / false_left` / `true_right / false_right`)

```

# Figures

## Figure 3 (without controls)

```{r}

# Means (Top Row) ====================================================================================================================

# agreeable headlines
fig3_agreeable_nc <- 
  svyby(~share_01, ~hdl_true + ~party_dem, 
        FUN = svymean, 
        design = svydesign(~1, 
                           weights = ~weight, 
                           data = m1nc_data)) %>%
  
  mutate(hdl_true = ifelse(hdl_true == 1, "True", "False")) %>%
  mutate(party_dem = ifelse(party_dem == 1, "Democrats", "Republicans")) %>%
  
  ggplot(aes(x = factor(hdl_true), 
             y = share_01, 
             ymin = share_01 - 1.96*se, 
             ymax = share_01 + 1.96*se,
             label = round(share_01, 2),
             fill = factor(party_dem))) +
  geom_bar(stat = "identity", position = position_dodge()) + 
  geom_errorbar(size = .3, width = .2) + 
  geom_text(aes(y = share_01 + 1.96*se), 
            nudge_y = .02, size = 3) + 
  facet_wrap(vars(party_dem)) + 
  theme_bw() + 
  scale_fill_manual(breaks = c("Democrats", "Republicans"), 
                    labels = c("Democrats", "Republicans"), 
                    values = c(cb_blue, cb_red), 
                    name = "") + 
  xlab("Headline Veracity") + 
  ylab("Mean Sharing Intention") + 
  ggtitle("Agreeable Headlines") + 
  ylim(c(0, .5)) + 
  theme(legend.position = "none") + 
  xlab("") + ylab("Mean Sharing Intention (0-1)") 

# disagreeable headlines
fig3_disagreeable_nc <- 
  svyby(~share_01, ~hdl_true + ~party_dem, 
        FUN = svymean, 
        design = svydesign(~1, 
                           weights = ~weight, 
                           data = m2nc_data)) %>%
  mutate(hdl_true = ifelse(hdl_true == 1, "True", "False")) %>%
  mutate(party_dem = ifelse(party_dem == 1, "Democrats", "Republicans")) %>%
  
  ggplot(aes(x = factor(hdl_true), 
             y = share_01,
             ymin = share_01 - 1.96*se,
             ymax = share_01 + 1.96*se,
             label = round(share_01, 2),
             fill = factor(party_dem))) +
  geom_bar(stat = "identity", position = position_dodge()) + 
  geom_errorbar(size = .3, width = .2) + 
  geom_text(aes(y = share_01 + 1.96*se), 
            nudge_y = .02, size = 3) + 
  facet_wrap(vars(party_dem)) + 
  theme_bw() + 
  scale_fill_manual(breaks = c("Democrats", "Republicans"), 
                    labels = c("Democrats", "Republicans"), 
                    values = c(cb_blue, cb_red), 
                    name = "") + 
  ggtitle("Disagreeable Headlines") + 
  ylim(c(0, .5)) + 
  theme(legend.position = "right") + 
  xlab("") + ylab("")


# Discernment (Middle Row) ===========================================================================================

# Disagreeable Headlines ----------------------------------------------------------------------------------------------

# Multiplicative Discernment (Agreeable)
fig3_multiplicative_discernment_agreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republicans",
                                   ifelse(str_detect(quantity, "left"), "Democrats", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Agreeable") %>%
  filter(quantity != "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = respondent_party, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none") + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Multiplicative Discernment") + ggtitle("") + ylab("Discernment") +
  theme(strip.text.x = element_blank()) 

# Multiplicative Discernment (Disagreeable)
fig3_multiplicative_discernment_disagreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republicans",
                                   ifelse(str_detect(quantity, "left"), "Democrats", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Disagreeable") %>%
  filter(quantity != "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = respondent_party, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  theme(legend.position = "none") + ggtitle("Multiplicative Discernment") + ggtitle("") + ylab("Discernment") +
  theme(strip.text.x = element_blank())

# Differences in Discernment (Bottom Row) ===========================================================================

# Disagreeable Headlines ----------------------------------------------------------------------------------------------

# Multiplicative Discernment (Agreeable)
fig3_multiplicative_differences_in_discernment_agreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republican",
                                   ifelse(str_detect(quantity, "left"), "Democrat", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Agreeable") %>%
  filter(quantity == "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = hdl_agreeable, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none", 
        axis.text.x = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Multiplicative Discernment") + ggtitle("") + 
  ylab("Ratio of Dem. Discernment \nRep. Discernment") + 
  theme(strip.text.x = element_blank()) 

# Multiplicative Discernment (Disagreeable)
fig3_multiplicative_differences_in_discernment_disagreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republican",
                                   ifelse(str_detect(quantity, "left"), "Democrat", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Disagreeable") %>%
  filter(quantity == "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = hdl_agreeable, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(axis.text.x = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  theme(legend.position = "none") + ggtitle("Multiplicative Discernment") + ggtitle("") + 
  ylab("Ratio of Dem. Discernment to \nRep Discernment") +
  theme(strip.text.x = element_blank())

# save combined figure =============================================================================

ggsave(
  (fig3_agreeable_nc + 
     fig3_disagreeable_nc + theme(legend.position = "none", legend.margin = margin(0, 7, 0, 0, "cm"))) / 
    
    ((fig3_multiplicative_discernment_agreeable_nc + 
        theme(legend.position = "none") +
        scale_y_continuous(limits = c(.7, 1.9), n.breaks = 10) +  
        fig3_multiplicative_discernment_disagreeable_nc + 
        scale_y_continuous(limits = c(.7, 1.9), n.breaks = 10, name = NULL))) /
    
    ((fig3_multiplicative_differences_in_discernment_agreeable_nc + 
        scale_y_continuous(limits = c(.6, 1.9), n.breaks = 10) +  
        fig3_multiplicative_differences_in_discernment_disagreeable_nc + 
        scale_y_continuous(limits = c(.6, 1.9), n.breaks = 10, name = NULL))) +
    
    theme(legend.position = "right", legend.margin = margin(0, 2, 0, 0, "cm")) +  
    plot_annotation(tag_levels = 'A'), 
  width = 11, height = 8, filename = here("figures", "main", "fig3.pdf"))


```

## Figure 3 (with controls)

```{r}

# Discernment (Middle Row) ===========================================================================================

# Disagreeable Headlines ----------------------------------------------------------------------------------------------

# Multiplicative Discernment (Agreeable)
fig3_multiplicative_discernment_agreeable_wc <- 
  rbind(
    summary(m1wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republicans",
                                   ifelse(str_detect(quantity, "left"), "Democrats", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Agreeable") %>%
  
  filter(quantity != "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = respondent_party, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none") + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Multiplicative Discernment") + ggtitle("") + ylab("Discernment") +
  theme(strip.text.x = element_blank()) 

# Multiplicative Discernment (Disagreeable)
fig3_multiplicative_discernment_disagreeable_wc <- 
  rbind(
    summary(m1wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republicans",
                                   ifelse(str_detect(quantity, "left"), "Democrats", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Disagreeable") %>%
  filter(quantity != "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = respondent_party, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment \n(True / False)", "Republican Discernment \n(True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  theme(legend.position = "none") + ggtitle("Multiplicative Discernment") + ggtitle("") + ylab("Discernment") +
  theme(strip.text.x = element_blank())

# Differences in Discernment (Bottom Row) ===========================================================================

# Disagreeable Headlines ----------------------------------------------------------------------------------------------

# Multiplicative Discernment (Agreeable)
fig3_multiplicative_differewces_in_discernment_agreeable_wc <- 
  rbind(
    summary(m1wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republican",
                                   ifelse(str_detect(quantity, "left"), "Democrat", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Agreeable") %>%
  filter(quantity == "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = hdl_agreeable, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none", 
        axis.text.x = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Multiplicative Discernment") + ggtitle("") + 
  ylab("Ratio of Dem. Discernment \nRep. Discernment") + 
  theme(strip.text.x = element_blank()) 

# Multiplicative Discernment (Disagreeable)
fig3_multiplicative_differewces_in_discernment_disagreeable_wc <- 
  rbind(
    summary(m1wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2wc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republican",
                                   ifelse(str_detect(quantity, "left"), "Democrat", NA))) %>%
  
  filter(discern_type == "Multiplicative Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left / false_left", "true_right / false_right", "ratio"))) %>%
  
  filter(hdl_agreeable == "Disagreeable") %>%
  filter(quantity == "ratio") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = hdl_agreeable, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 1, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left / false_left", "true_right / false_right", "ratio"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment / Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(axis.text.x = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  theme(legend.position = "none") + ggtitle("Multiplicative Discernment") + ggtitle("") + 
  ylab("Ratio of Dem. Discernment to \nRep Discernment") +
  theme(strip.text.x = element_blank())

# save combined figure =============================================================================

ggsave(
  (fig3_agreeable_nc + 
     fig3_disagreeable_nc + theme(legend.position = "none", legend.margin = margin(0, 7, 0, 0, "cm"))) / 
    
    ((fig3_multiplicative_discernment_agreeable_wc + 
        theme(legend.position = "none") +
        scale_y_continuous(limits = c(.7, 1.9), n.breaks = 10) +  
        fig3_multiplicative_discernment_disagreeable_wc + 
        scale_y_continuous(limits = c(.7, 1.9), n.breaks = 10, name = NULL))) /
    
    ((fig3_multiplicative_differewces_in_discernment_agreeable_wc + 
        scale_y_continuous(limits = c(.6, 1.9), n.breaks = 10) +  
        fig3_multiplicative_differewces_in_discernment_disagreeable_wc + 
        scale_y_continuous(limits = c(.6, 1.9), n.breaks = 10, name = NULL))) + 
    
    
    
    theme(legend.position = "right", legend.margin = margin(0, 2, 0, 0, "cm")) +  
    plot_annotation(tag_levels = 'A'), 
  width = 11, height = 8, filename = here("figures", "sm", "fig3_b.pdf"))

```

## Figure 3 (additive discernment, without controls)

```{r}

# Discernment (Middle Row) ===========================================================================================

# Disagreeable Headlines ----------------------------------------------------------------------------------------------

# Additive Discernment (Agreeable)
fig3_additive_discernment_agreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republicans",
                                   ifelse(str_detect(quantity, "left"), "Democrats", NA))) %>%
  
  filter(discern_type == "Additive Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left - false_left", "true_right - false_right", "diff"))) %>%
  
  filter(hdl_agreeable == "Agreeable") %>%
  filter(quantity != "diff") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = respondent_party, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment \n(True - False)", "Republican Discernment \n(True - False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment \n(True - False)", "Republican Discernment \n(True - False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none", 
        panel.grid.major = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Additive Discernment") + ggtitle("") + ylab("Discernment") +
  theme(strip.text.x = element_blank()) 

# Additive Discernment (Disagreeable)
fig3_additive_discernment_disagreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republicans",
                                   ifelse(str_detect(quantity, "left"), "Democrats", NA))) %>%
  
  filter(discern_type == "Additive Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left - false_left", "true_right - false_right", "diff"))) %>%
  
  filter(hdl_agreeable == "Disagreeable") %>%
  filter(quantity != "diff") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = respondent_party, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment \n(True - False)", "Republican Discernment \n(True - False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment \n(True - False)", "Republican Discernment \n(True - False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none", 
        panel.grid.major = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Additive Discernment") + ggtitle("") + ylab("Discernment") +
  theme(strip.text.x = element_blank()) 

# Differences in Discernment (Bottom Row) ===========================================================================

# Disagreeable Headlines ----------------------------------------------------------------------------------------------

# Additive Discernment (Agreeable)
fig3_additive_differences_in_discernment_agreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republican",
                                   ifelse(str_detect(quantity, "left"), "Democrat", NA))) %>%
  
  filter(discern_type == "Additive Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left - false_left", "true_right - false_right", "diff"))) %>%
  
  filter(hdl_agreeable == "Agreeable") %>%
  filter(quantity == "diff") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = hdl_agreeable, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment (True - False)", "Republican Discernment (True - False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none", 
        axis.text.x = element_blank(), 
        panel.grid.major = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Additive Discernment") + ggtitle("") + 
  ylab("Ratio of Dem. Discernment \nRep. Discernment") + 
  theme(strip.text.x = element_blank()) 

# Additive Discernment (Disagreeable)
fig3_additive_differences_in_discernment_disagreeable_nc <- 
  rbind(
    summary(m1nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Agreeable"),
    summary(m2nc_transformed) %>% as.data.frame() %>% mutate(hdl_agreeable = "Disagreeable")) %>%
  rownames_to_column("quantity") %>% mutate(quantity = str_remove(string = quantity, pattern = "1")) %>%
  mutate(discern_type = ifelse(str_detect(quantity, "-|diff"), "Additive Discernment", 
                               ifelse(str_detect(quantity, "/|ratio"), "Multiplicative Discernment", NA))) %>%
  na.omit() %>%
  mutate(respondent_party = ifelse(str_detect(quantity, "right"), "Republican",
                                   ifelse(str_detect(quantity, "left"), "Democrat", NA))) %>%
  
  filter(discern_type == "Additive Discernment") %>%
  mutate(quantity = factor(quantity, levels = c("true_left - false_left", "true_right - false_right", "diff"))) %>%
  
  filter(hdl_agreeable == "Disagreeable") %>%
  filter(quantity == "diff") %>%
  
  ggplot(aes(y = Estimate, 
             ymin = `2.5 %`,
             ymax = `97.5 %`, 
             x = hdl_agreeable, 
             col = quantity, 
             shape = quantity)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_pointrange(position = position_dodge(.3)) + 
  theme_bw() + 
  xlab("") + ylab("") + 
  facet_wrap(vars(hdl_agreeable), scales = "free_x") + 
  scale_color_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment (True / False)", "Republican Discernment (True / False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(cb_blue, cb_red, cb_black))  + 
  scale_shape_manual(name = "", 
                     breaks = c( "true_left - false_left", "true_right - false_right", "diff"), 
                     labels = c("Democratic Discernment (True - False)", "Republican Discernment (True - False)", "Democratic Discernment - Republican Discernment"), 
                     values = c(1, 2, 15))  + 
  
  theme(legend.position = "none", 
        axis.text.x = element_blank(), 
        panel.grid.major = element_blank()) + 
  guides(col=guide_legend(nrow=3), 
         shape = guide_legend(nrow = 3)) + 
  ggtitle("Additive Discernment") + ggtitle("") + 
  ylab("Ratio of Dem. Discernment \nRep. Discernment") + 
  theme(strip.text.x = element_blank()) 

# save combined figure =============================================================================

ggsave(
  (fig3_agreeable_nc + 
     fig3_disagreeable_nc + theme(legend.position = "none", legend.margin = margin(0, 7, 0, 0, "cm"))) / 
    
    ((fig3_additive_discernment_agreeable_nc + 
        theme(legend.position = "none") +
        scale_y_continuous(limits = c(-.02, .2), n.breaks = 10) +  
        fig3_additive_discernment_disagreeable_nc + 
        scale_y_continuous(limits = c(-.02, .2), n.breaks = 10, name = NULL))) /
    
    ((fig3_additive_differences_in_discernment_agreeable_nc + 
        scale_y_continuous(limits = c(-.1, .2), n.breaks = 10) +  
        fig3_additive_differences_in_discernment_disagreeable_nc + 
        scale_y_continuous(limits = c(-.1, .2), n.breaks = 10, name = NULL))) + 
    
    theme(legend.position = "right", legend.margin = margin(0, 2, 0, 0, "cm")) +  
    plot_annotation(tag_levels = 'A'), 
  width = 11, height = 8, filename = here("figures", "sm", "fig3_c.pdf"))

```

## Treatment Effect by Headline (All Interventions)

```{r}

# All Accuracy Prompts (Pooled) ================================================

# create nested data -----------------------------------------------------------

m3_data <- 
  long %>% 
  mutate(party_demrep = ifelse(pid7 < 4, "dem", 
                               ifelse(pid7 > 4, "rep", NA))) %>%
  filter(!is.na(pid7) & pid7 != 4) %>%
  group_by(hdl_id, party_demrep, hdl_fakeReal) %>%
  nest()

m3_fun <- function(df){
  glm(share ~ pooledTreat,
      family = "gaussian", weights = weight, data = df)
}

# run models -------------------------------------------------------------------
m3_models <- 
  m3_data %>%
  mutate(model = map(data, m3_fun))

tbl_hdl <- 
  long %>%
  dplyr::select(hdl_id, rating_all_likelyTrue, rating_dem_likelyTrue, rating_rep_likelyTrue,
                hdl_fakeReal, hdl_demRep) %>%
  group_by(hdl_id) %>%
  slice(1) %>%
  ungroup()

tbl_coef <- 
  m3_models %>%
  rowwise() %>%
  mutate(pooledTreat_coef = list(broom::tidy(model)[broom::tidy(model)$term == "pooledTreat",])) %>%
  unnest(pooledTreat_coef) 

tbl_combined <-
  inner_join(tbl_hdl, tbl_coef, by = "hdl_id") %>%
  mutate(rating_all_likelyTrue_ownparty = ifelse(party_demrep == "dem", rating_dem_likelyTrue, 
                                                 ifelse(party_demrep == "rep", rating_rep_likelyTrue, NA)), 
         hdl_true = ifelse(hdl_fakeReal.x == "Real", "True Headlines",
                           ifelse(hdl_fakeReal.x == "Fake", "False Headlines", NA)), 
         hdl_agreeable = ifelse(party_demrep == hdl_demRep, 1, 0))

# create figures ----------------------------------------------------------------

fig_dem <- 
  tbl_combined %>%
  filter(party_demrep == "dem") %>%
  ggplot(aes(x = rating_all_likelyTrue_ownparty, 
             y = estimate,
             col = party_demrep)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_point(alpha = .5) + 
  theme_bw() + 
  theme(legend.position = "none") + 
  scale_color_manual(breaks = c("dem", "rep"), 
                     values = c("blue", "red"), 
                     labels = c("Democrats", "Republicans"), 
                     name = "") + 
  geom_smooth(method = "lm", se = F, inherit.aes = F, size = 2, 
              aes(x = rating_all_likelyTrue, 
                  y = estimate, 
                  col = party_demrep)) +
  xlab("Perceived Veracity of Headlines\nAmong Democrats") + 
  ylab("Effect of Treatment\nOn Sharing Intentions (0-1)") + 
  ggtitle("Democratic Respondents") + 
  geom_text(aes(x = 2, y = 0.5), col = "blue", label = "β = 0.0230") + 
  ylim(c(-1, .6)) + 
  xlim(c(1, 5))

fig_rep <- 
  tbl_combined %>%
  filter(party_demrep == "rep") %>%
  ggplot(aes(x = rating_all_likelyTrue_ownparty, 
             y = estimate,
             col = party_demrep)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_point(alpha = .5) + 
  theme_bw() + 
  theme(legend.position = "none") + 
  scale_color_manual(breaks = c("dem", "rep"), 
                     values = c("blue", "red"), 
                     labels = c("Democrats", "Republicans"), 
                     name = "") + 
  geom_smooth(method = "lm", se = F, inherit.aes = F, size = 2,
              aes(x = rating_all_likelyTrue, 
                  y = estimate, 
                  col = party_demrep)) +
  xlab("Perceived Veracity of Headlines\nAmong Republicans") + 
  ylab("") + 
  ggtitle("Republican Respondents") + 
  geom_text(aes(x = 2, y = 0.5), col = "red", label = "β = 0.0236") + 
  ylim(c(-1, .6)) + 
  xlim(c(1, 5))

# combine plots
ggsave(fig_dem + fig_rep + 
         plot_annotation(tag_levels = 'A'), 
       width = 10, height = 5, filename = here("figures", "main",  "fig_slopes.pdf"))

# get slopes and test for differences between them ---------------------------

tmp_data <- 
  long %>% 
  dplyr::select(share_01, hdl_true, party_dem, weight, caseid, hdl_id, pooledTreat) %>%
  na.omit() %>%
  inner_join(tbl_hdl, test_data, by = "hdl_id")


# beta = 0.0230 for Dems
summary(glm(share_01 ~ pooledTreat*rating_all_likelyTrue,
            family = "gaussian", weights = weight, 
            data = tmp_data %>% filter(party_dem == 1)))

# beta = 0.0236 for Reps
summary(glm(share_01 ~ pooledTreat*rating_all_likelyTrue,
            family = "gaussian", weights = weight, 
            data = tmp_data %>% filter(party_dem == 0)))

# interaction (beta = -0.0006) is not statisticaly significant (p = .95)
summary(glm(share_01 ~ pooledTreat*rating_all_likelyTrue*party_dem,
            family = "gaussian", weights = weight, data = tmp_data))


```

## Treatment Effect by Headline (Evaluation Interventions Only)

```{r}

# Evaluation Accuracy Prompt Only ================================================

# create nested data -----------------------------------------------------------

m4_data <- 
  long %>% 
  filter(cond_int %in% c("control", "headline", "headlineFeedback")) %>%
  mutate(party_demrep = ifelse(pid7 < 4, "dem", 
                               ifelse(pid7 > 4, "rep", NA))) %>%
  filter(!is.na(pid7) & pid7 != 4) %>%
  group_by(hdl_id, party_demrep, hdl_fakeReal) %>%
  nest()

m4_fun <- function(df){
  glm(share ~ pooledTreat,
      family = "gaussian", weights = weight, data = df)
}

# run models -------------------------------------------------------------------
m4_models <- 
  m4_data %>%
  mutate(model = map(data, m4_fun))

tbl_hdl <- 
  long %>%
  dplyr::select(hdl_id, rating_all_likelyTrue, rating_dem_likelyTrue, rating_rep_likelyTrue,
                hdl_fakeReal, hdl_demRep) %>%
  group_by(hdl_id) %>%
  slice(1) %>%
  ungroup()

tbl_coef <- 
  m4_models %>%
  rowwise() %>%
  mutate(pooledTreat_coef = list(broom::tidy(model)[broom::tidy(model)$term == "pooledTreat",])) %>%
  unnest(pooledTreat_coef) 

tbl_combined <-
  inner_join(tbl_hdl, tbl_coef, by = "hdl_id") %>%
  mutate(rating_all_likelyTrue_ownparty = ifelse(party_demrep == "dem", rating_dem_likelyTrue, 
                                                 ifelse(party_demrep == "rep", rating_rep_likelyTrue, NA)), 
         hdl_true = ifelse(hdl_fakeReal.x == "Real", "True Headlines",
                           ifelse(hdl_fakeReal.x == "Fake", "False Headlines", NA)), 
         hdl_agreeable = ifelse(party_demrep == hdl_demRep, 1, 0))


# create figures ----------------------------------------------------------------

fig_dem <- 
  tbl_combined %>%
  filter(party_demrep == "dem") %>%
  ggplot(aes(x = rating_all_likelyTrue_ownparty, 
             y = estimate,
             col = party_demrep)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_point(alpha = .5) + 
  theme_bw() + 
  theme(legend.position = "none") + 
  scale_color_manual(breaks = c("dem", "rep"), 
                     values = c("blue", "red"), 
                     labels = c("Democrats", "Republicans"), 
                     name = "") + 
  geom_smooth(method = "lm", se = F, inherit.aes = F, size = 2, 
              aes(x = rating_all_likelyTrue, 
                  y = estimate, 
                  col = party_demrep)) +
  xlab("Perceived Veracity of Headlines\nAmong Democrats") + 
  ylab("Effect of Treatment\nOn Sharing Intentions (0-1)") + 
  ggtitle("Democratic Respondents") + 
  geom_text(aes(x = 2, y = 0.5), col = "blue", label = "B = 0.0141") + 
  ylim(c(-1.2, .8)) + 
  xlim(c(1, 5))

fig_rep <- 
  tbl_combined %>%
  filter(party_demrep == "rep") %>%
  ggplot(aes(x = rating_all_likelyTrue_ownparty, 
             y = estimate,
             col = party_demrep)) + 
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_point(alpha = .5) + 
  theme_bw() + 
  theme(legend.position = "none") + 
  scale_color_manual(breaks = c("dem", "rep"), 
                     values = c("blue", "red"), 
                     labels = c("Democrats", "Republicans"), 
                     name = "") + 
  geom_smooth(method = "lm", se = F, inherit.aes = F, size = 2,
              aes(x = rating_all_likelyTrue, 
                  y = estimate, 
                  col = party_demrep)) +
  xlab("Perceived Veracity of Headlines\nAmong Republicans") + 
  ylab("") + 
  ggtitle("Republican Respondents") + 
  geom_text(aes(x = 2, y = 0.5), col = "red", label = "B = 0.0297") + 
  ylim(c(-1.2, .8)) + 
  xlim(c(1, 5))

# combine plots
fig_dem + fig_rep

ggsave(fig_dem + fig_rep + 
         plot_annotation(tag_levels = 'A'), 
       width = 10, height = 5, filename = here("figures", "sm",  "fig_slopes_evaluation_only.pdf"))

# get slopes and test for differences between them ---------------------------

tmp_data <- 
  long %>% 
  filter(cond_int %in% c("control", "headline", "headlineFeedback")) %>%
  dplyr::select(share_01, hdl_true, party_dem, weight, caseid, hdl_id, pooledTreat) %>%
  na.omit() %>%
  inner_join(tbl_hdl, test_data, by = "hdl_id")


# beta = 0.0141 for Dems
summary(glm(share_01 ~ pooledTreat*rating_all_likelyTrue,
            family = "gaussian", weights = weight, 
            data = tmp_data %>% filter(party_dem == 1)))

# beta = 0.0297 for Reps
summary(glm(share_01 ~ pooledTreat*rating_all_likelyTrue,
            family = "gaussian", weights = weight, 
            data = tmp_data %>% filter(party_dem == 0)))

# interaction (beta = -0.0156) is not statisticaly significant (p = .225)
summary(glm(share_01 ~ pooledTreat*rating_all_likelyTrue*party_dem,
            family = "gaussian", weights = weight, data = tmp_data))

```

## Pretest Figure

```{r}

mturk_df <- 
  long %>%
  dplyr::select(hdl_id, hdl_fakeReal, hdl_demRep, 
                rating_dem_betterForReps, rating_rep_betterForReps, rating_dem_likelyTrue, 
                rating_rep_likelyTrue, rating_all_betterForReps, rating_all_likelyTrue) %>%
  gather(key, value, -hdl_id, -hdl_fakeReal, -hdl_demRep) %>%
  mutate(rater = ifelse(str_detect(key, "rating_all"), "all", 
                        ifelse(str_detect(key, "rating_dem"), "dem", 
                               ifelse(str_detect(key, "rating_rep"), "rep", "other")))) %>%
  mutate(hdl_fakeReal = ifelse(hdl_fakeReal == "Fake", "False Headlines", "True Headlines")) %>%
  mutate(key = str_remove(key, "[^_]*_[^_]*")) %>%
  filter(key %in% c("_betterForReps", "_likelyTrue")) %>%
  distinct() 

# Partisan Rating =============================================================

fig_partisan_density <- 
  mturk_df %>%
  filter(key == "_betterForReps") %>%
  filter(!str_detect(hdl_id, "COVID")) %>%
  filter(rater != "all") %>%
  na.omit() %>%
  ggplot(aes(x = value, 
             fill = rater)) +
  geom_density(alpha = .3, linetype = "blank") + 
  ggtitle("Distribution of\nHeadline Partisanship Ratings") + 
  ylab("Density") +
  xlab("Headline Partisanship Rating\n(1 = Left-leaning, 6 = Right-leaning)") + 
  theme_minimal() + 
  scale_fill_manual(name = "Rater Party",
                    values = c("gray", "#00BFC4", "#F8766D"), 
                    breaks = c("all", "dem", "rep"),
                    labels = c("All", "Democrats", "Republicans"), 
                    guide = guide_legend(direction = "horizontal")) + 
  theme(legend.position = "none", 
        legend.text = element_text(size=6),
        legend.title = element_text(size = 7), 
        legend.background = element_blank(), 
        legend.box.background = element_rect(colour = "black"), 
        legend.key.size = unit(0.35, "cm"), 
        plot.title = element_text(face = "bold", hjust = 0.5)) + 
  ylim(c(0, .4)) + 
  xlim(c(1, 6)) 

with(mturk_df %>%
       filter(!str_detect(hdl_id, "COVID"), 
              key == "_betterForReps") %>%
       spread(key = rater, val = value), 
     cor(dem, rep))

fig_partisan_dot <- 
  mturk_df %>%
  filter(!str_detect(hdl_id, "COVID"), 
         key == "_betterForReps") %>%
  spread(key = rater, val = value) %>%
  ggplot(aes(x = dem, 
             y = rep)) + 
  geom_point(size = 4, alpha = .8) + 
  theme_minimal() + 
  labs(x = "Democrat Raters",
       y = "Republican Raters",
       title = "Headline Partisanship Ratings",
       subtitle = "(1 = Left-leaning, 6 = Right-leaning)") + 
  xlim(c(1, 6)) + 
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),  
        plot.subtitle = element_text(hjust = 0.5))

ggarrange(fig_partisan_density, 
          fig_partisan_dot, 
          nrow = 2)

# Headline Veracity Rating ==============================================================

fig_veracity_density <- 
  mturk_df %>%
  filter(key == "_likelyTrue") %>%
  filter(rater != "all") %>%
  na.omit() %>%
  ggplot(aes(x = value, 
             fill = factor(rater))) +
  geom_density(alpha = .3, linetype = "blank") + 
  ggtitle("Distribution of\nHeadline Veracity Ratings") + 
  xlab("Headline Veracity Ratings\n (1 = False, 6 = True)") + 
  ylab("Density") + 
  theme_minimal() + 
  theme(legend.position = c(.85, .85), 
        legend.text = element_text(size=6),
        legend.title = element_text(size = 7), 
        legend.background = element_blank(), 
        legend.box.background = element_rect(colour = "black"), 
        legend.key.size = unit(0.35, "cm"), 
        plot.title = element_text(face = "bold", hjust = 0.5)) + 
  facet_wrap(vars(hdl_fakeReal), 
             labeller = labeller(hdl_fakeReal = c(real = "Snopes: Real Headline", 
                                                  fake = "Snopes: Fake Headline")), 
             nrow = 2) + 
  scale_fill_manual(name = "Rater Party",
                    values = c("gray", "#00BFC4", "#F8766D"), 
                    breaks = c("all", "dem", "rep"),
                    labels = c("All", "Democrats", "Republicans")) + 
  xlim(c(1,6)) 

with(mturk_df %>%
       filter(!str_detect(hdl_id, "COVID"), 
              key == "_likelyTrue") %>%
       spread(key = rater, val = value), 
     cor(dem, rep)) #r = .74

fig_veracity_dot <-
  mturk_df %>%
  filter(!str_detect(hdl_id, "COVID"), 
         key == "_likelyTrue") %>%
  spread(key = rater, val = value) %>%
  ggplot(aes(x = dem, 
             y = rep, 
             col = hdl_fakeReal)) + 
  geom_point(size = 4, alpha = .8) + 
  theme_minimal() + 
  labs(x = "Democrat Raters",
       y = "Republican Raters",
       title = "Headline Veracity Ratings",
       subtitle = "(1 = False, 6 = True)") + 
  scale_color_manual(name = "Snopes Rating",
                     values = c("chartreuse3", "orange"),
                     breaks = c("True Headlines", "False Headlines"),
                     labels = c("True", "False"), 
                     guide = guide_legend(direction = "horizontal")) + 
  theme(legend.position = c(.85, .85),
        legend.text = element_text(size=6),
        legend.title = element_text(size = 7),
        plot.title = element_text(hjust = 0.5, face = "bold"), 
        plot.subtitle = element_text(hjust = 0.5),
        legend.background = element_blank(), 
        legend.box.background = element_rect(colour = "black"), 
        legend.key.size = unit(0.35, "cm")) +  
  guides(col = guide_legend(nrow = 2)) + 
  ylim(c(1,6)) + 
  xlim(c(1,6))

ggsave(
  (fig_partisan_density + fig_veracity_density) / 
    (fig_partisan_dot + fig_veracity_dot) +
    plot_annotation(tag_levels = 'A'), 
  width = 8, height = 8, filename = here("figures", "main",  "fig_pretest.pdf"))

```

# Tables


## Table 1: Models from Figure 3 with No Controls

```{r}

stargazer::stargazer(
  m1nc_all, m1nc_true, m1nc_false, type = "latex")

stargazer::stargazer(
  m2nc_all, m2nc_true, m2nc_false, type = "latex")

stargazer::stargazer(
  m1wc_all, m1wc_true, m1wc_false, type = "latex")

stargazer::stargazer(
  m2wc_all, m2wc_true, m2wc_false, type = "latex")

```

# P Values for Main Text

```{r}

# Figure 3 p values ...............................................

# agreeable headlines
summary(m1nc_transformed, parm = "true_left / false_left", null = 1)
summary(m1nc_transformed, parm = "true_right / false_right", null = 1)
summary(m1nc_transformed, parm = "ratio", null = 1)

# disagreeable headlines
summary(m2nc_transformed, parm = "true_left / false_left", null = 1)
summary(m2nc_transformed, parm = "true_right / false_right", null = 1)
summary(m2nc_transformed, parm = "ratio", null = 1)

```
